#include "savethread.h"

SaveThread::SaveThread(QObject *parent)
    : QThread(parent)
    , m_bStart(true)
{

}
//将执行任务动作放入线程执行
void SaveThread::run()
{
    while (m_bStart) {//用来停止

        SaveTask *task = popTask();
        if(task){
            if(task->saveImage()){
                emit sigWorkFinish(task->path());
                delete task;//用完删除
                task = nullptr;
            }
        }else{
            delete task;
        }
        QThread::msleep(50);
    }
}
//向任务队列添加任务
void SaveThread::addTask(SaveTask *task)
{
    m_mutex.lock();
    m_taskList.push_front(task);
    m_mutex.unlock();
}
//判断任务队列里是否有任务，并把任务拿出来执行
SaveTask *SaveThread::popTask()
{
    m_mutex.lock();
    SaveTask *task;
    if(0 == m_taskList.size())
    {
        task = nullptr;
    }
    else
    {
        task = m_taskList.takeFirst();
    }
    m_mutex.unlock();
    return task;
}
